package org.jeecg.modules.demo.report1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.report1.entity.*;
import org.jeecg.modules.demo.report1.mapper.GjshYingshouMapper;
import org.jeecg.modules.demo.report1.mapper.YingshouMapper;
import org.jeecg.modules.demo.report1.service.IGjshYingshouService;
import org.jeecg.modules.demo.report1.service.IYingshouService;
import org.jeecg.modules.demo.summary.entity.MonthlySummary;
import org.jeecg.modules.demo.summary.service.IMonthlySummaryService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 应收报表
 * @Author: jeecg-boot
 * @Date:   2024-04-01
 * @Version: V1.0
 */
@Service
public class GjshYingshouServiceImpl extends ServiceImpl<GjshYingshouMapper, GjshReport> implements IGjshYingshouService {
    @Override
    public Xin setTotalValue(List<GjshReport> list) {
        return null;
    }
    @Autowired
    private GjshYingshouMapper gjshYingshouMapper;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private IMonthlySummaryService monthlySummaryService;

    /**
     * 返回的动态表头列
     * @param communityName
     * @param columnTableList
     * @return
     */
    public List<Columns> getColumns(String communityName, List<FreeType> columnTableList){
        List<Columns> resultColumnsList = new ArrayList<>();
        resultColumnsList.add(new Columns("社区名称","center","communityName_dictText",100));
        resultColumnsList.add(new Columns("房产编号","center","propertyNumber",80));
        resultColumnsList.add(new Columns("业主名称","center","ownerName",80));
        resultColumnsList.add(new Columns("账期","center","accountingPeriod",100));
        for (FreeType column : columnTableList) {
            resultColumnsList.add(new Columns(column.getName(),"center",column.getId(),100));
        }
        //添加合计列
        resultColumnsList.add(new Columns("合计","center","合计",100));
        return resultColumnsList;
    }

    /**
     * 返回的动态数据
     * @param communityName
     * @param columnTableList
     * @param resultColumnsList
     * @return
     */
    public List<Map<String,Object>> getResultDataSource(String communityName, List<FreeType> columnTableList, List<Columns> resultColumnsList){
        //查询当前小区下的数据
//		List<GjshReport> gjshReportList = gjshYingshouMapper.selectAll();
        QueryWrapper<GjshReport> gjshReportQueryWrapper = new QueryWrapper<>();
        gjshReportQueryWrapper.eq("community_id",communityName);
        List<GjshReport> gjshReports = gjshYingshouMapper.selectList(gjshReportQueryWrapper);
        //每个人的数据集合
        Map<String,Map> everyOneMapList = new LinkedHashMap<>();
        List<Map<String, Object>> tableData = new ArrayList<>();
        //房产编号
        String exampleHouseNumber = "";
        for (GjshReport gjshReport : gjshReports) {
            //表格项
            Map<String,Object> tableItem = new LinkedHashMap<>();
            //设置当前房产编号 与下次循环作比较
            exampleHouseNumber = gjshReport.getHouseNumber();
            //社区名称
            String communityId = gjshReport.getCommunityId();
            //房产编号
            String houseNumber = gjshReport.getHouseNumber();
            //业主姓名
            String ownerName = gjshReport.getOwnerName();
            //账期ID
            String accountPeriodId = gjshReport.getAccountPeriodId();
            //将ID转为月份
            QueryWrapper<MonthlySummary> monthlySummaryQueryWrapper = new QueryWrapper<>();
            monthlySummaryQueryWrapper.eq("id",accountPeriodId);
            //获取月份
            List<MonthlySummary> monthlySummaryList = monthlySummaryService.list(monthlySummaryQueryWrapper);
            String month = monthlySummaryList.get(0).getMonth();

            //收费项目
            String gjshFeeTypeId = gjshReport.getGjshFeeTypeId();
            //收费金额
            BigDecimal amount = gjshReport.getAmount();

            //判断map中是否已经有该键
            if (everyOneMapList.containsKey(gjshReport.getHouseNumber())){
                //已经有该客户的逻辑 遍历动态表头
                for (FreeType freeType : columnTableList) {
                    if (gjshReport.getGjshFeeTypeId().equals(freeType.getId())){
//						tableItem.put(gjshFeeTypeId,amount);
                        everyOneMapList.get(houseNumber).put(gjshFeeTypeId,amount);
                        break;
                    }
                }
                continue;
            }
            // map中没有该客户的逻辑
            int i = 0;
            for (Columns columns : resultColumnsList) {
                //设置社区Id
                if (i == 0){
//					tableItem.put(columns.getDataIndex(),communityId);
                    //获取社区名称
                    QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
                    departQueryWrapper.eq("id",communityId);
                    List<SysDepart> departList = departService.list(departQueryWrapper);
                    String departName = departList.get(0).getDepartName();
                    tableItem.put(columns.getDataIndex(),departName);
                    i++;
                    continue;
                }
                //设置房产编号
                if (i == 1){
                    tableItem.put(columns.getDataIndex(),houseNumber);
                    i++;
                    continue;
                }
                //设置业主姓名
                if (i == 2){
                    tableItem.put(columns.getDataIndex(),ownerName);
                    i++;
                    continue;
                }
                //设置账期ID
                if (i == 3){
                    tableItem.put(columns.getDataIndex(),month);
                    i++;
                    continue;
                }
                if ( i>= 4){
                    if (gjshReport.getGjshFeeTypeId().equals(columns.getDataIndex())){
                        tableItem.put(gjshFeeTypeId,amount);
                        break;
                    }else {
                        i++;
                    }
                }
            }
            //往容器内添加数据
            //往总集合内添加数据
            everyOneMapList.put(houseNumber,tableItem);
        }
        List<Map<String,Object>> resultType = new ArrayList<>();
        // 直接遍历 everyOneMapList 的 entrySet()
        for (Map.Entry<String, Map> entry : everyOneMapList.entrySet()) {
            Map<String, Object> innerMap = entry.getValue();
            //添加每一项
            resultType.add(innerMap);
        }
        int y = 0;
        //遍历resultType
        for (Map<String, Object> map : resultType) {
            // 输出当前 Map 对象的内容
            BigDecimal total = BigDecimal.ZERO;
            int i = 0;
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (i >= 4){
                    String key = entry.getKey(); // 获取键
                    BigDecimal value = (BigDecimal)entry.getValue(); // 获取值
                    total = total.add(value);
                    // 在此处处理键值对，例如打印或进行其他逻辑操作
                    System.out.println("Key: " + key + ", Value: " + value);
                }
                if (i == map.size()-1){
                    map.put("合计",total);
                    resultType.get(y).put("合计",total);
                }
                i++;
            }
            y++;
        }
        return resultType;
    }

    @Override
    public Map getResultTotal(List<Map<String,Object>> resultDataSource, List<FreeType> columnTableList) {
        columnTableList.add(new FreeType("合计","合计"));
        List<Map<String,Object>> bigDecimalList = new ArrayList<>();
        Map<String,Object> itemMap = new HashMap<>();
        int size = columnTableList.size();
        //遍历表头数据
        for (int i = 0; i < size; i++) {
            BigDecimal total0 = BigDecimal.ZERO;
//            Map itemMap = new HashMap<>();
            //将每一项添加进去
            for (Map map : resultDataSource) {
                //判断空值
                if (map.containsKey(columnTableList.get(i).getId())){
                    BigDecimal total1 = (BigDecimal)map.get(columnTableList.get(i).getId());
                    total0 = total0.add(total1);
                }
            }
            itemMap.put(columnTableList.get(i).getId(),total0);
        }
        System.out.println(bigDecimalList);
        return itemMap;
    }
}
